package com.filtter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import javax.xml.crypto.Data;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class LoggingFilter implements Filter {
    private SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //1.放行前处理
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
        String requestURI = httpServletRequest.getRequestURI();
        String format = sdf.format(new Date());
        long start = System.currentTimeMillis();
        System.out.println(format+"--->"+requestURI);

        //2.放行
//        filterChain.doFilter(servletRequest,servletResponse);


        //3.放行后相应前处理
        long end = System.currentTimeMillis();
        String format1 = sdf.format(new Date());
        System.out.println(format1+"--->"+requestURI+"--->耗时:"+(end-start));
    }
}
